package com.csp.main.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csp.common.utils.R;
import com.csp.main.entity.Commodity;
import com.csp.main.entity.UserCollect;
import com.csp.main.service.CommodityService;
import com.csp.main.service.UserCollectService;
import com.csp.main.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 收藏 前端控制器
 * </p>
 *
 * @author csp
 * @since 2022-06-09
 */
@RestController
@RequestMapping("/main/user-collect")
public class UserCollectController {
    @Autowired
    private UserCollectService userCollectService;

    @GetMapping("/list")
    public R list(HttpServletRequest request){
        String token = request.getHeader(JWTUtils.USER_LOGIN_TOKEN);
        String userId = JWTUtils.validateToken(token);

        QueryWrapper<UserCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<UserCollect> list = userCollectService.list(queryWrapper);
        return R.ok().data("rows",list);
    }

    @PostMapping("/add/{comId}")
    public R add(HttpServletRequest request,@PathVariable String comId){
        String token = request.getHeader(JWTUtils.USER_LOGIN_TOKEN);
        String userId = JWTUtils.validateToken(token);
        UserCollect userCollect = new UserCollect();
        userCollect.setUserId(userId);
        userCollect.setCollectId(comId);
        boolean save = userCollectService.save(userCollect);
        return save?R.ok():R.error();
    }

    @DeleteMapping("delete")
    public R delete(String id){
        boolean b = userCollectService.removeById(id);
        return b?R.ok():R.error();
    }
}

